<?php

class WoguAdsAction extends CommonAction
{
    public function baidu() {
        $data = urlget('我要长高');
    }
    
    public function google() {
        $data = google_urlget3('small cabinet');
    }
}

function urlget($kw) {
    $cookiefile = ADMIN_PATH . '/baidu.cookie';
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => 'http://www.baidu.com/s?ie=utf-8&wd='.urlencode($kw),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_COOKIEFILE => $cookiefile,
        CURLOPT_COOKIEJAR => $cookiefile,
        CURLOPT_REFERER => 'http://www.baidu.com',
        CURLOPT_HTTPHEADER => array(
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip,deflate,sdch',
            'Accept-Language' => 'zh-CN,zh;q=0.8,en;q=0.6,id;q=0.4,ja;q=0.2,zh-TW;q=0.2',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36',
        ),
    ));
    $data = curl_exec($ch);
    curl_close($ch);
    if(preg_match_all('/<div class="(result.+c-container.+?)"/i', $data, $matches)) {
        $count = 0;
        $haspic = false;
        foreach($matches[1] as $v) {
            if($v != 'result-op c-container xpath-log') {
                $count++;
            } else {
                $haspic = true;
                break;
            }
        }
        if(!$haspic) {
            echo '没有出现百度图片<br/>';
        } else {
            echo '百度图片上方共有'.$count.'个普通链接<br/>';
        }
    }

    if(preg_match_all('/bdfs\d{1}/i', $data, $matches)) {
        $rightnum = count($matches[0]) / 2;
        echo '右侧推广数:'.$rightnum.'个<br/>';
    }
}

function google_urlget($kw) {
    //$data = file_get_contents('http://www.google.com/search?q='.urlencode($kw).'&bav=on.2,or.r_cp.r_qf.&fp=1&bvm=pv.xjs.s.en_US.vwsQ5GX-QL0.O&tch=1&ech=1&psi=kIkWVPGWO8bCiQKv8oD4AQ.1410763149480.3');
    $arr = explode('/*""*/', $data);
    $chrs = include 'chrs.php';
    foreach($arr as $v) {
        if(!empty($v)) {
    //        for($i = 0; $i < strlen($v); $i++) {
    //            $chr = $v[$i];
    //            if(!empty($chr) && !iconv('utf-8', 'gbk', $chr)) {
    //                echo $chr."\r\n";
    //                $chrs[] = $chr;
    //            }
    //        }
            if(!empty($chrs)) {
                /*file_put_contents('chrs.php', "<?php\r\nreturn ".var_export($chrs, true).";\r\n?>");*/
                $v = str_replace($chrs, ' ', $v);
            }
            $obj = json_decode($v);
            echo $obj->d;
        }
    }
}

function google_urlget2($kw) {
    $data = file_get_contents('http://www.google.com/search?q='.urlencode($kw));
    //$data = file_get_contents('1.txt');
    //echo $data;
    //exit;

    $topads = $rightads = $totalads = $sponsorednum = 0;
    if(preg_match('/<div id="tvcap">.+<ol>(.+?)<\/ol>/', $data, $matches)) {
        if(preg_match_all('/<li class="ads-ad"/', $matches[1], $matches1)) {
            $topads = count($matches1[0]);
            echo $topads;
            echo "\r\n";
        }
    }
    
    if(preg_match_all('/<li class="ads-ad"/', $data, $matches2)) {
        $totalads = count($matches2[0]);
    }
    
    $rightads = $totalads - $topads;
    echo $rightads;
    echo "\r\n";
    
    if(preg_match_all('/<li class="g">|<li class="g" id="imagebox_bigimages">/', $data, $matches3)) {
        $count = 0;
        $haspic = false;
        foreach($matches3[0] as $v) {
            if($v != '<li class="g" id="imagebox_bigimages">') {
                $count++;
            } else {
                $haspic = true;
                break;
            }
        }
        if(!$haspic) {
            echo '没有出现Google图片'."\r\n";
        } else {
            echo 'Google图片上方共有'.$count.'个普通链接'."\r\n";
        }
    }
    
    if(preg_match('/class="_Sb"/', $data, $matches4)) {
        $sponsorednum = count($matches4[0]);
        echo $sponsorednum."\r\n";
    }
}

function google_urlget3($kw) {
    $data = file_get_contents('http://www.google.com/search?q='.urlencode($kw));

    $topads = $rightads = $sponsorednum = 0;
    if(preg_match('/<div id="tads".+<ol.*?>(.+?)<\/ol>/', $data, $matches)) {
        if(preg_match_all('/<li class="taf"/', $matches[1], $matches1)) {
            $topads = count($matches1[0]);
            echo $topads;
            echo "\r\n";
        }
    }

    if(preg_match('/<ol class="nobr"[^>]*>(.+?)<\/ol>/', $data, $matches2)) {
        if(preg_match_all('/<a id="an\d+"/', $matches2[1], $matches5)) {
            $rightads = count($matches5[0]);
            echo "rightads={$rightads}\r\n";
        }
    }
    
    if(preg_match_all('/<li class="g">.+?<\/li>/', $data, $matches3)) {
        $count = 0;
        $haspic = false;
        foreach($matches3[0] as $v) {
            if(preg_match('/class="s"/', $v)) {
                $count++;
            } else {
                $haspic = true;
                break;
            }
        }
        if(!$haspic) {
            echo '没有出现Google图片'."\r\n";
        } else {
            echo 'Google图片上方共有'.$count.'个普通链接'."\r\n";
        }
    }
    
    if(preg_match('/13px">Sponsored<a/', $data, $matches4)) {
        $sponsorednum = count($matches4[0]);
        echo "sponsorednum={$sponsorednum}\r\n";
    }
}